package com.chineseall.app.service;

import java.util.concurrent.TimeUnit;

import javax.annotation.PostConstruct;

import jmind.core.util.RandUtil;

import com.chineseall.rebirth.api.RebirthApiFactory;
import com.chineseall.rebirth.api.helper.Const;
import com.chineseall.rebirth.api.pojo.BusEvent;
import com.chineseall.rebirth.api.service.Callback;
import com.chineseall.rebirth.api.service.IBusService;

public class TestEventService {

    IBusService service = RebirthApiFactory.getFactory().getApiService(IBusService.class);
    private int driver = 4322;
    int bookId = 10080;

    @PostConstruct
    public void init() {

        Thread t = new Thread(new Runnable() {
            public void run() {
                while (true) {
                    behavior();
                    try {
                        TimeUnit.SECONDS.sleep(30);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        });
        t.setDaemon(true);
        t.start();

    }

    public void testSubscribe() {
        service.subscribe(Const.EventType.createChapter.name(), true, new Callback() {

            @Override
            public boolean doIt(BusEvent event) {
                System.out.println(event.getTopic());
                System.out.println(event.demark());
                return true;
            }

        });
    }

    /**
     * 创建章节
     * 2013-11-18
     */
    public void createChapter() {
        BusEvent event = new BusEvent(Const.EventType.createChapter.name());
        event.setDriver(driver); // 作者id
        event.setPassenger(bookId); // 书id
        event.mark("bookName", "书名");
        event.mark("chapterId", 1); // 章节id
        event.mark("q", "内容");
        event.mark("ip", "用户ip");
        service.notify(event);
    }

    // 评论
    public void comment() {
        BusEvent event = new BusEvent(Const.EventType.comment.name());
        event.setDriver(driver); // 登录用户id，事件产生着
        event.setPassenger(134432); // 书id
        event.mark("commentId", 1983776); // 评论id
        if (RandUtil.randomBoolean())
            event.mark("rid", 7075872); // 当时评论回复时，回复评论的评论id
        event.mark("ruid", 4322); // 当时评论回复时，回复评论对应的用户uid
        event.mark("content", "this is content");
        service.notify(event);
    }

    /**
     * 订阅
     * 2013-11-18
     */
    public void subscription() {
        BusEvent event = new BusEvent(Const.EventType.subscription.name());
        event.setDriver(driver); // 登录用户id，即订阅人id
        event.setPassenger(bookId); // 书id

        service.notify(event);
    }

    /**
     * 加入书架
     * 2013-11-18
     */
    public void bookshelf() {
        BusEvent event = new BusEvent(Const.EventType.bookshelf.name());
        event.setDriver(driver); // 登录用户id，
        event.setPassenger(bookId); // 书id
        service.notify(event);
    }

    /**
     * 
     * 2013-11-18
     */
    public void behavior() {
        BusEvent event = new BusEvent(Const.EventType.behavior.name());
        event.setDriver(driver); // 登录用户id，
        event.setPassenger(bookId); // 书id
        event.mark("type", "flower"); // 鲜花
        service.notify(event);

        event = new BusEvent(Const.EventType.behavior.name());
        event.setDriver(driver); // 登录用户id，
        event.setPassenger(bookId); // 书id
        event.mark("type", "gb"); // 贵宾
        service.notify(event);

        event = new BusEvent(Const.EventType.behavior.name());
        event.setDriver(driver); // 登录用户id，
        event.setPassenger(bookId); // 书id
        event.mark("type", "pm"); // 盖章
        service.notify(event);

        event = new BusEvent(Const.EventType.behavior.name());
        event.setDriver(driver); // 登录用户id，
        event.setPassenger(bookId); // 书id
        event.mark("type", "pk"); // 凹凸票
        event.mark("value", RandUtil.randomBoolean() ? 1 : -1);
        service.notify(event);

    }

}
